﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TradeMeSDK;

namespace TradeMo
{
    public partial class BuyingView 
    {
        private readonly BuyingViewModel _viewModel;

        public BuyingView()
        {
            InitializeComponent();
            _viewModel = new BuyingViewModel();
            DataContext = _viewModel;
            AnimationContext = LayoutRoot;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            _viewModel.LoadWatchList();
            _viewModel.LoadWonList();
            _viewModel.LoadLostList();
        }

        private void WatchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (WatchListBox.SelectedItem == null) return;
            var selected = WatchListBox.SelectedItem as SearchResultItem;
            WatchListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);
        }

        private void WonListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (WonListBox.SelectedItem == null) return;
            var selected = WonListBox.SelectedItem as SearchResultItem;
            WonListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);
        }

        private void LostListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (LostListBox.SelectedItem == null) return;
            var selected = LostListBox.SelectedItem as SearchResultItem;
            LostListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);
        }


        private void OpenListing(int listingId)
        {
            NavigationService.Navigate(new Uri("/ListingView.xaml?ListingId=" + listingId, UriKind.Relative));
        }

    }
}